package com.example.restaurant.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.Result;
import com.example.restaurant.controller.form.AddDishTypeForm;
import com.example.restaurant.controller.form.UpdateDishTypeForm;
import com.example.restaurant.model.Dish;
import com.example.restaurant.model.DishType;
import com.example.restaurant.service.DishTypeService;
import com.example.restaurant.service.param.AddDishTypeParam;
import com.example.restaurant.service.param.UpdateDishTypeParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 菜类表 前端控制器
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@RestController
@Slf4j
public class DishTypeController {

    @Resource
    private DishTypeService dishTypeService;

    //查询商户旗下的所有菜类，分页
    @GetMapping("getDishType")
    @Log
    public Result getDishType( @RequestParam(defaultValue = "1")int current, @RequestParam(defaultValue = "5") int size){
        Object userId = StpUtil.getExtra("userId");
        log.info("{}",userId);
        String userId1 = String.valueOf(userId);
        int i = Integer.parseInt(userId1);
       log.info("{},{},{}",userId,current,size);
        log.info("查询指令");
        Page<DishType> dishType = dishTypeService.getDishType(i, current, size);
        return Result.success(dishType);
    }

    //修改菜类
    @PutMapping("modifyDishType")
    @Log
    public Result modifyDishType(@RequestBody @Valid UpdateDishTypeForm updateDishTypeForm){
        log.info("{}",updateDishTypeForm);
        UpdateDishTypeParam updateDishTypeParam = BeanUtil.copyProperties(updateDishTypeForm, UpdateDishTypeParam.class);
        boolean b = dishTypeService.modifyDishType(updateDishTypeParam);
        return Result.success();
    }

    //新增菜类
    @PostMapping("addDishType")
    @Log
    public Result addDishType(@RequestBody @Valid AddDishTypeForm addDishTypeForm){
        log.info("{}",addDishTypeForm.toString());
        Object userId = StpUtil.getExtra("userId");
        String userId1 = String.valueOf(userId);
        int i = Integer.parseInt(userId1);
        addDishTypeForm.setUserId(i);
        AddDishTypeParam addDishTypeParam = BeanUtil.copyProperties(addDishTypeForm, AddDishTypeParam.class);
        boolean b = dishTypeService.addDishType(addDishTypeParam);
        return Result.success();
    }

    //删除菜类，根据菜类唯一id
    @DeleteMapping("delDishType")
    @Log
    public Result delDishType( int dishTypeId){
        log.info("{}",dishTypeId);
        boolean b = dishTypeService.delDishType(dishTypeId);
        return Result.success();
    }

    //通过门店ID查询菜类信息
    @GetMapping("/getDishTypeByStoreId")
    public Result getDishTypeByStoreId(int storeId){
        List<DishType> dishTypes = dishTypeService.getDishTypeById(storeId);
        return Result.success(dishTypes);
    }

    //根据门店id和模糊菜品名,模糊查询菜品
    @GetMapping("selDishTypeByName")
    @Log
    public Result selDishTypeByName(int storeId,String dishTypeName,@RequestParam(defaultValue = "1")Integer current,@RequestParam(defaultValue = "5")Integer size){
       Page<DishType> dishTypePage = dishTypeService.selDishTypeByName(storeId, dishTypeName, current, size);
        return Result.success(dishTypePage);
    }


}
